<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.company.DepartmentComInfo" %>
<%@ page import="weaver.hrm.company.SubCompanyComInfo" %>
<%@ page import="weaver.hrm.job.JobActivitiesComInfo" %>
<%@ page import="weaver.hrm.job.JobGroupsComInfo" %>
<%@ page import="weaver.hrm.job.JobTitlesComInfo" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%
    String type = request.getParameter("type");
    out.print("========================开始同步组织结构====================</br>");
    RecordSet rs = new RecordSet();
    RecordSetDataSource rsd = new RecordSetDataSource("OLDOA");
    if ("1".equals(type)) {
        boolean flag = rs.executeUpdate("delete HrmJobGroups");
        out.print(">>>>>删除历史数据成功</br>");
        if (flag) {
            out.print(">>>>>>>>>>>>>>>>>>开始同步人力资源职务类型表(HrmJobGroups)</br>");
            rsd.execute("select * from HrmJobGroups where 1=1");
            int counts = rsd.getCounts();
            int count = 0;
            while (rsd.next()) {
                String insertSql = "insert into HrmJobGroups ";
                //遍历所有字段
                String[] colnames = rsd.getColumnName();
                StringBuilder fields = new StringBuilder();
                StringBuilder values = new StringBuilder();
                for (String colname : colnames) {
                    colname = colname.toLowerCase();
                    fields.append(colname).append(",");
                    String value = Util.null2String(rsd.getString(colname));
                    values.append("'").append(value).append("',");
                }
                fields.deleteCharAt(fields.length() - 1);
                values.deleteCharAt(values.length() - 1);
                insertSql += "(" + fields + ") values (" + values + ")";
                out.print(">>>>>开始同步数据>>>>>insertSql=" + insertSql + "</br>");
                if (rs.executeUpdate(insertSql)) {
                    count++;
                }
            }
            JobGroupsComInfo jc = new JobGroupsComInfo();
            jc.removeCache();
            out.print(">>>>>结束同步人力资源职务类型表(HrmJobGroups)>>>>>总计数量=" + counts + ",完成数量=" + count + "</br>");
        }
    } else if ("2".equals(type)) {
        boolean flag = rs.executeUpdate("delete HrmJobActivities");
        out.print(">>>>>删除历史数据成功</br>");
        if (flag) {
            out.print(">>>>>>>>>>>>>>>>>>开始同步人力资源职务表(HrmJobActivities)</br>");
            rsd.execute("select * from HrmJobActivities where 1=1");
            int counts = rsd.getCounts();
            int count = 0;
            while (rsd.next()) {
                String insertSql = "insert into HrmJobActivities ";
                //遍历所有字段
                String[] colnames = rsd.getColumnName();
                StringBuilder fields = new StringBuilder();
                StringBuilder values = new StringBuilder();
                for (String colname : colnames) {
                    colname = colname.toLowerCase();
                    fields.append(colname).append(",");
                    String value = Util.null2String(rsd.getString(colname));
                    values.append("'").append(value).append("',");
                }
                fields.deleteCharAt(fields.length() - 1);
                values.deleteCharAt(values.length() - 1);
                insertSql += "(" + fields + ") values (" + values + ")";
                out.print(">>>>>开始同步数据>>>>>insertSql=" + insertSql + "</br>");
                if (rs.executeUpdate(insertSql)) {
                    count++;
                }
            }
            JobActivitiesComInfo jc = new JobActivitiesComInfo();
            jc.removeJobActivitiesCache();
            out.print(">>>>>结束同步人力资源职务表(HrmJobActivities)>>>>>总计数量=" + counts + ",完成数量=" + count + "</br>");
        }
    } else if ("3".equals(type)) {
        boolean flag = rs.executeUpdate("delete hrmjobtitles");
        out.print(">>>>>删除历史数据成功</br>");
        if (flag) {
            out.print(">>>>>>>>>>>>>>>>>>开始同步岗位表(hrmjobtitles)</br>");
            rsd.execute("select * from hrmjobtitles where 1=1");
            int counts = rsd.getCounts();
            int count = 0;
            while (rsd.next()) {
                String insertSql = "insert into hrmjobtitles ";
                //遍历所有字段
                String[] colnames = rsd.getColumnName();
                StringBuilder fields = new StringBuilder();
                StringBuilder values = new StringBuilder();
                for (String colname : colnames) {
                    colname = colname.toLowerCase();
                    fields.append(colname).append(",");
                    String value = Util.null2String(rsd.getString(colname));
                    values.append("'").append(value).append("',");
                }
                fields.deleteCharAt(fields.length() - 1);
                values.deleteCharAt(values.length() - 1);
                insertSql += "(" + fields + ") values (" + values + ")";
                out.print(">>>>>开始同步数据>>>>>insertSql=" + insertSql + "</br>");
                if (rs.executeUpdate(insertSql)) {
                    count++;
                }
            }
            JobTitlesComInfo jc = new JobTitlesComInfo();
            jc.removeJobTitlesCache();
            out.print(">>>>>结束同步岗位表(hrmjobtitles)>>>>>总计数量=" + counts + ",完成数量=" + count + "</br>");
        }
    } else if ("4".equals(type)) {
        boolean flag = rs.executeUpdate("delete HrmSubCompany ");
        out.print(">>>>>删除历史数据成功</br>");
        if (flag) {
            out.print(">>>>>>>>>>>>>>>>>>开始同步分部表(HrmSubCompany)</br>");
            rsd.execute("select * from HrmSubCompany where 1=1");
            int counts = rsd.getCounts();
            int count = 0;
            while (rsd.next()) {
                String insertSql = "insert into HrmSubCompany ";
                //遍历所有字段
                String[] colnames = rsd.getColumnName();
                StringBuilder fields = new StringBuilder();
                StringBuilder values = new StringBuilder();
                for (String colname : colnames) {
                    colname = colname.toLowerCase();
                    if ("test".equals(colname)) {
                        continue;
                    }
                    fields.append(colname).append(",");
                    String value = Util.null2String(rsd.getString(colname));
                    values.append("'").append(value).append("',");

                }
                fields.deleteCharAt(fields.length() - 1);
                values.deleteCharAt(values.length() - 1);
                insertSql += "(" + fields + ") values (" + values + ")";
                out.print(">>>>>开始同步数据>>>>>insertSql=" + insertSql + "</br>");
                if (rs.executeUpdate(insertSql)) {
                    count++;
                }
            }
            SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
            subCompanyComInfo.removeCache();
            out.print(">>>>>结束同步分部表(HrmJobGroups)>>>>>总计数量=" + counts + ",完成数量=" + count + "</br>");
        }
    } else if ("5".equals(type)) {
        boolean flag = rs.executeUpdate("delete HrmDepartment ");
        out.print(">>>>>删除历史数据成功</br>");
        if (flag) {
            out.print(">>>>>>>>>>>>>>>>>>开始同步部门表(HrmDepartment)</br>");
            rsd.execute("select * from HrmDepartment where 1=1");
            int counts = rsd.getCounts();
            int count = 0;
            while (rsd.next()) {
                String insertSql = "insert into HrmDepartment ";
                //遍历所有字段
                String[] colnames = rsd.getColumnName();
                StringBuilder fields = new StringBuilder();
                StringBuilder values = new StringBuilder();
                for (String colname : colnames) {
                    colname = colname.toLowerCase();
                    fields.append(colname).append(",");
                    String value = Util.null2String(rsd.getString(colname));
                    values.append("'").append(value).append("',");
                }
                fields.deleteCharAt(fields.length() - 1);
                values.deleteCharAt(values.length() - 1);
                insertSql += "(" + fields + ") values (" + values + ")";
                out.print(">>>>>开始同步数据>>>>>insertSql=" + insertSql + "</br>");
                if (rs.executeUpdate(insertSql)) {
                    count++;
                }
            }
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            departmentComInfo.removeCache();
            out.print(">>>>>结束同步部门表(HrmDepartment)>>>>>总计数量=" + counts + ",完成数量=" + count + "</br>");
        }
    } else if ("6".equals(type)) {
        boolean flag = rs.executeUpdate("delete hrmresource ");
        out.print(">>>>>删除历史数据成功</br>");
        if (flag) {
            out.print(">>>>>>>>>>>>>>>>>>开始同步人力资源表(hrmresource)</br>");
            rsd.execute("select * from hrmresource where 1=1");
            int counts = rsd.getCounts();
            int count = 0;
            while (rsd.next()) {
                String insertSql = "insert into hrmresource ";
                //遍历所有字段
                String[] colnames = rsd.getColumnName();
                StringBuilder fields = new StringBuilder();
                StringBuilder values = new StringBuilder();
                for (String colname : colnames) {
                    colname = colname.toLowerCase();
                    if ("notallot".equals(colname)) {
                        continue;
                    }
                    if("beforefrozen".equals(colname)){
                        continue;
                    }
                    if("resourcefrom".equals(colname)){
                        continue;
                    }
                    if("isnewuser".equals(colname)){
                        continue;
                    }

                    fields.append(colname).append(",");
                    String value = Util.null2String(rsd.getString(colname));
                    values.append("'").append(value).append("',");
                }
                fields.deleteCharAt(fields.length() - 1);
                values.deleteCharAt(values.length() - 1);
                insertSql += "(" + fields + ") values (" + values + ")";
                out.print(">>>>>开始同步数据>>>>>insertSql=" + insertSql + "</br>");
                if (rs.executeUpdate(insertSql)) {
                    count++;
                }
            }
            try {
                ResourceComInfo rci = new ResourceComInfo();
                rci.removeResourceCache();
            } catch (Exception e) {
                e.printStackTrace();
            }
            out.print(">>>>>结束同步人力资源表(hrmresource)>>>>>总计数量=" + counts + ",完成数量=" + count + "</br>");
        }
    } else {
        out.print(">>>>>输入参数错误，type类型为1，2，3，4，5，6，请依次输入</br>");
    }
    out.print("========================结束同步组织结构====================</br>");
%>
